import { changePwd } from '@/static/js/user'
export default {
	data() {
		return {
			formData: [{
				label: "旧密码",
				value: "",
				showPassword: false,
				placeholder: "请输入密码",
				maxlength: 30,
				show: false // 是否显示
			}, {
				label: "新密码",
				value: "",
				showPassword: false,
				placeholder: "请输入新密码",
				maxlength: 30,
				show: true
			}, {
				label: "再次输入",
				value: "",
				showPassword: false,
				placeholder: "请再次输入新密码",
				maxlength: 30,
				show: true
			}],
		};
	},
	methods: {
		// 切换密码显示
		togglePasswordVisibility(item) {
			item.showPassword = !item.showPassword;
		},
		submit() {
			const { value: oldpass } = this.formData[0];
			const { value: newpass } = this.formData[1];
			const { value: newpass2 } = this.formData[2];

			if (!this.$tools.IsEmpty(oldpass) && this.isEdit) { return this.$tools.msg('请输入旧密码') };
			if (!this.$tools.IsEmpty(newpass)) { return this.$tools.msg('请输入新密码') };
			if (!this.$tools.IsEmpty(newpass2)) { return this.$tools.msg('请再次输入新密码') };
			if (newpass !== newpass2) { return this.$tools.msg('两次密码输入不一致') };

			let data = {
				oldpass,
				newpass,
				newpass2,
			}
			if (!this.formData[0].show) {
				delete data.oldpass
			}
			changePwd(data).then(res => {
				if (res.code == '1') {
					this.$tools.appBack(res.msg)
				} else {
					that.$tools.msg(res.msg);
				}
			})
		},
	},

	onLoad(e) {
		if (e.type == 2) {
			this.formData[0].show = true;
		}
	},

}